<template>
  <div class="params-container">
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>参数列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 提示区域 -->
      <el-alert
        title="注意：只允许第三级分类设置相关参数！"
        type="warning"
        show-icon
        :closable="false"
      >
      </el-alert>
      <!-- 选择三级属性展示参数和属性 -->
      <el-row>
        <el-col :span="5"><span>选择商品分类：</span></el-col>
        <el-col :span="12">
          <el-cascader
            v-model="selectedKeys"
            :options="categoriesList"
            :props="{
              expandTrigger: 'hover',
              ...categoriesListProps
            }"
            @change="handleChange"
            :show-all-levels="false"
            clearable
          ></el-cascader>
        </el-col>
      </el-row>

      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="动态参数" name="many">
          <el-button
            type="primary"
            size="mini"
            :disabled="buttonState"
            @click="openDialog"
            >添加参数</el-button
          >
          <ParamsTable
            :data="dynamicProps"
            :activeName="activeName"
          ></ParamsTable>
        </el-tab-pane>
        <el-tab-pane label="静态属性" name="only">
          <el-button
            type="primary"
            size="mini"
            :disabled="buttonState"
            @click="openDialog"
            >添加属性</el-button
          >
          <ParamsTable
            :data="dynamicProps"
            :activeName="activeName"
          ></ParamsTable>
        </el-tab-pane>
      </el-tabs>
      <AddDialog
        :sel="activeName"
        :id="selectedKeys[selectedKeys.length - 1]"
        @updateList="getParamsData"
      ></AddDialog>

      <EditDialog
        :sel="activeName"
        :id="selectedKeys[selectedKeys.length - 1]"
        @updateList="getParamsData"
      ></EditDialog>
    </el-card>
  </div>
</template>

<script>
import AddDialog from '@/components/goods_params/AddDialog.vue'
import EditDialog from '@/components/goods_params/EditDialog.vue'
import ParamsTable from '@/components/goods_params/ParamsTable.vue'
export default {
  name: 'Params',
  data() {
    return {
      activeName: 'many',
      categoriesList: [],
      selectedKeys: [],
      categoriesListProps: {
        label: 'cat_name',
        value: 'cat_id',
        children: 'children'
      },
      dynamicProps: [],
      formProp: {
        attr_name: [
          { required: true, message: '请输入动态参数！', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 获取联级选择器的数据
    async getCategories() {
      const { data: res } = await this.$http.get('categories')

      this.categoriesList = res.data
    },
    // 联级选择器选择后调用获取商品参数方法
    handleChange() {
      this.getParamsData()
    },
    // tab栏切换后根据tab栏的名称切换不同的数据列表
    handleClick() {
      this.getParamsData()
    },
    // 获取动态参数/静态属性方法
    async getParamsData() {
      const { data: res } = await this.$http.get(
        `categories/${
          this.selectedKeys[this.selectedKeys.length - 1]
        }/attributes`,
        {
          params: { sel: this.activeName }
        }
      )

      if (res.meta.status !== 200) return (this.dynamicProps = [])
      // 将数据的tag字符串分割成数组遍历循环成tag标签
      res.data.forEach((item) => {
        item.attr_vals = item.attr_vals === '' ? [] : item.attr_vals.split(',')
        // 为每一个数据设置单独的文本框状态属性，和添加的tag文本属性
        this.$set(item, 'addNewTagInputVisible', false)
        this.$set(item, 'newTagVals', '')
      })
      this.dynamicProps = res.data
    },
    // 打开添加动态参数/静态属性对话框方法
    openDialog() {
      this.$store.commit('params/addDialogVisibleState', true)
    }
  },
  created() {
    this.getCategories()
  },
  computed: {
    buttonState() {
      return !(this.selectedKeys.length === 3)
    }
  },
  components: { AddDialog, EditDialog, ParamsTable }
}
</script>

<style lang="less" scoped>
.el-alert {
  margin-bottom: 15px;
}
.el-tag {
  margin: 5px;
}
.el-row {
  display: flex;
  align-items: center;
}

.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
</style>
